//
//  YFKaiDianVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/19.
//  Copyright © 2018年 apple. All rights reserved.
//
#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"

#import "GPDateView.h"
#import "YFKaiDianVC.h"
#import "QJKDVCThree.h"
#import "YFKaiDianHeaderView.h"

//#import "QJMineAddressSelectView.h"
#import "QJKDCategorySelectView.h"
#import "SOPopViewTool.h"
#import "YFMapLocationVC.h"
#import "YFUserStatusAndTypeTool.h"
#import "UIButton+WebCache.h"

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import "YFWebViewController.h"
@interface YFKaiDianVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    // -1  未找到店铺   新用户注册就是这个
    NSInteger _userState; //  0 未审核  1 审核通过  2 审核未通过
    NSInteger _user_type;  // 1 是 泉界会员，2 是泉界创客，3，是泉界商家
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YFKaiDianHeaderView *headerView;
@property (nonatomic, strong) KaiDianModel *kdModel;
@property (nonatomic, strong) ChaKanDianPuModel *ckdpModel;
@property (nonatomic, strong) QJKDCategorySelectView *categoryView;
//@property (nonatomic, strong) QJMineAddressSelectView *selectView;

@property (nonatomic, strong) UIImage *frontImg;
@property (nonatomic, strong) UIImage *backImg;

@property (nonatomic, strong) UIImage *shopImg1;
@property (nonatomic, strong) UIImage *shopImg2;
@property (nonatomic, strong) UIImage *shopImg3;
@property (nonatomic, strong) UIImage *yyImg;

//@property (nonatomic, strong) NSString *longitudeStr; //经度
//@property (nonatomic, strong) NSString *latitudeStr;  //纬度

//@property (weak, nonatomic) IBOutlet UITextField *shopName;
//@property (weak, nonatomic) IBOutlet UILabel *category;
//@property (weak, nonatomic) IBOutlet UIButton *chooseBtn;
//@property (weak, nonatomic) IBOutlet UITextField *phone;
//@property (weak, nonatomic) IBOutlet UITextField *shopkeeperName;
//@property (weak, nonatomic) IBOutlet UITextField *address;
//
//@property (weak, nonatomic) IBOutlet UIButton *locationBtn;
//@property (weak, nonatomic) IBOutlet UILabel *coordinate;
//@property (weak, nonatomic) IBOutlet UITextField *legalEntityName;
//@property (weak, nonatomic) IBOutlet UITextField *legalEntityPhone;
//@property (weak, nonatomic) IBOutlet UITextField *accountNumber;
//
//@property (weak, nonatomic) IBOutlet UIButton *iDCardFront;
//@property (weak, nonatomic) IBOutlet UIButton *iDCardBack;
//@property (weak, nonatomic) IBOutlet UIButton *shopImgView1;
//@property (weak, nonatomic) IBOutlet UIButton *shopImgView2;
//@property (weak, nonatomic) IBOutlet UIButton *shopImgView3;
//@property (weak, nonatomic) IBOutlet UIButton *agreement;
//
//@property (weak, nonatomic) IBOutlet UIButton *nextStep;

@property (nonatomic, strong) NSString *idcardType;

@end

@implementation YFKaiDianVC

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type  == YFKaiDianVCType_KaiDian) {
        self.title = @"申请开店";
    }
    if (self.type == YFKaiDianVCType_Edit) {
        self.title = @"编辑店铺";
        [self requestData1];
    }
    if (self.type == YFKaiDianVCType_See) {
        self.title = @"查看店铺";
        [self requestData];
        
    }
    
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 初始化model
    self.kdModel = [[KaiDianModel alloc] init];
    
    //    [YFSaveTool userDefaults:@"rootVC" key:@"KD"];
    
    
    
    // Do any additional setup after loading the view from its nib.
}
- (void)requestData1{
    
    
    NSDictionary *dic = [NSMutableDictionary new];
    [dic setValue:@"" forKey:@"sign"];
    
    [WWNetRequest fetchShopkeeperMessWithParameters:dic success:^(id response) {
        
        if (response) {
            
            NSDictionary *dict =  [response objectForKey:@"data"];
            _ckdpModel = [[ChaKanDianPuModel alloc] initWithDictionary:dict error:nil];
            
            if ([_ckdpModel.bankcard isKindOfClass:[NSDictionary class]]) {
                
                _headerView.accountNumber.text =  [_ckdpModel.bankcard objectForKey:@"card_num"];//结算账号
                _headerView.legalEntityPhone.text = [_ckdpModel.bankcard objectForKey:@"mobile"];//字段有问题
                
            }
            _headerView.companyName.text = _ckdpModel.company_name;//店铺名称
            _headerView.category.text = _ckdpModel.category_near_id;//店铺分类
            _headerView.phone.text = _ckdpModel.tel;//店铺电话
            _headerView.linkName.text = _ckdpModel.link_name;//店铺姓名
            NSString *address = [NSString stringWithFormat:@"%@-%@-%@",_ckdpModel.provice,_ckdpModel.city,_ckdpModel.region];
            [_headerView.addressBtn setTitle:address forState:UIControlStateNormal];//地址
            _headerView.addressDetail.text = _ckdpModel.detail_address;
            _headerView.coordinate.text = [NSString stringWithFormat:@"%@ %@",_ckdpModel.latitude,_ckdpModel.longitude];
            _headerView.legalEntityName.text = _ckdpModel.legal_person;
            _headerView.cardNum.text = _ckdpModel.company_license_code;
            
            NSString *cardFront = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.id_card_url];
            [_headerView.iDCardFront sd_setImageWithURL:[NSURL URLWithString:cardFront] forState:UIControlStateNormal];
            NSString *cardBack = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.id_card_url2];
            [_headerView.iDCardBack sd_setImageWithURL:[NSURL URLWithString:cardBack] forState:UIControlStateNormal];
            
            NSString *cardImg = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.company_license_url];
            [_headerView.cardImgView sd_setImageWithURL:[NSURL URLWithString:cardImg] forState:UIControlStateNormal];
            
            NSString *shopImg1 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            //             NSString *shopImg2 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            //             NSString *shopImg3 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            [_headerView.shopImgView1 sd_setImageWithURL:[NSURL URLWithString:shopImg1] forState:UIControlStateNormal];
            //            [_headerView.shopImgView2 sd_setImageWithURL:[NSURL URLWithString:shopImg2] forState:UIControlStateNormal];
            //            [_headerView.shopImgView3 sd_setImageWithURL:[NSURL URLWithString:shopImg3] forState:UIControlStateNormal];
            
            _headerView.companyName.userInteractionEnabled = NO;
            _headerView.category.userInteractionEnabled = NO;
            
            _headerView.linkName.userInteractionEnabled = NO;
            _headerView.addressBtn.userInteractionEnabled =NO;
            
            _headerView.coordinate.userInteractionEnabled = NO;
            _headerView.legalEntityName.userInteractionEnabled = NO;
            _headerView.legalEntityPhone.userInteractionEnabled = NO;
            _headerView.accountNumber.userInteractionEnabled = NO;
            _headerView.iDCardBack.userInteractionEnabled = NO;
            _headerView.iDCardFront.userInteractionEnabled = NO;
            _headerView.cardImgView.userInteractionEnabled = NO;
            
            
             _headerView.chooseBtn.hidden = YES;
            _headerView.agreeBtn.hidden = YES;
            _headerView.agreenLabel.hidden = YES;
            _headerView.agreement.hidden = YES;
           
        }
        
    } failure:^(NSDictionary *error) {
        
    }];
}
- (void)requestData{
    
    
    NSDictionary *dic = [NSMutableDictionary new];
    [dic setValue:@"" forKey:@"sign"];
    
    [WWNetRequest fetchShopkeeperMessWithParameters:dic success:^(id response) {
        
        if (response) {
            
            NSDictionary *dict =  [response objectForKey:@"data"];
            _ckdpModel = [[ChaKanDianPuModel alloc] initWithDictionary:dict error:nil];
            
            if ([_ckdpModel.bankcard isKindOfClass:[NSDictionary class]]) {
                
                _headerView.accountNumber.text =  [_ckdpModel.bankcard objectForKey:@"card_num"];//结算账号
                _headerView.legalEntityPhone.text = [_ckdpModel.bankcard objectForKey:@"mobile"];//字段有问题
                
            }
            _headerView.companyName.text = _ckdpModel.company_name;//店铺名称
            _headerView.category.text = _ckdpModel.category_near_id;//店铺分类
            _headerView.phone.text = _ckdpModel.tel;//店铺电话
            _headerView.linkName.text = _ckdpModel.link_name;//店铺姓名
            NSString *address = [NSString stringWithFormat:@"%@-%@-%@",_ckdpModel.provice,_ckdpModel.city,_ckdpModel.region];
            [_headerView.addressBtn setTitle:address forState:UIControlStateNormal];//地址
            _headerView.addressDetail.text = _ckdpModel.detail_address;
            _headerView.coordinate.text = [NSString stringWithFormat:@"%@ %@",_ckdpModel.latitude,_ckdpModel.longitude];
            _headerView.legalEntityName.text = _ckdpModel.legal_person;
            _headerView.cardNum.text = _ckdpModel.company_license_code;
            
            NSString *cardFront = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.id_card_url];
            [_headerView.iDCardFront sd_setImageWithURL:[NSURL URLWithString:cardFront] forState:UIControlStateNormal];
            NSString *cardBack = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.id_card_url2];
            [_headerView.iDCardBack sd_setImageWithURL:[NSURL URLWithString:cardBack] forState:UIControlStateNormal];
            
            NSString *cardImg = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.company_license_url];
            [_headerView.cardImgView sd_setImageWithURL:[NSURL URLWithString:cardImg] forState:UIControlStateNormal];
            
            NSString *shopImg1 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            //             NSString *shopImg2 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            //             NSString *shopImg3 = [NSString stringWithFormat:@"%@%@",NET_BASEURL,_ckdpModel.image_url_thumb];
            [_headerView.shopImgView1 sd_setImageWithURL:[NSURL URLWithString:shopImg1] forState:UIControlStateNormal];
            //            [_headerView.shopImgView2 sd_setImageWithURL:[NSURL URLWithString:shopImg2] forState:UIControlStateNormal];
            //            [_headerView.shopImgView3 sd_setImageWithURL:[NSURL URLWithString:shopImg3] forState:UIControlStateNormal];
            
            
            _headerView.companyName.userInteractionEnabled = NO;
            _headerView.category.userInteractionEnabled = NO;
            _headerView.phone.userInteractionEnabled = NO;
            _headerView.linkName.userInteractionEnabled = NO;
            _headerView.addressBtn.userInteractionEnabled = NO;
            _headerView.addressDetail.userInteractionEnabled = NO;
            _headerView.coordinate.userInteractionEnabled = NO;
            _headerView.legalEntityName.userInteractionEnabled = NO;
            _headerView.legalEntityPhone.userInteractionEnabled = NO;
            _headerView.accountNumber.userInteractionEnabled = NO;
            _headerView.iDCardFront.userInteractionEnabled = NO;
            _headerView.iDCardBack.userInteractionEnabled = NO;
            _headerView.shopImgView1.userInteractionEnabled = NO;
            _headerView.cardImgView.userInteractionEnabled = NO;
            _headerView.locationBtn.hidden = YES;
            _headerView.nextStep.hidden = YES;
            _headerView.agreeBtn.hidden = YES;
            _headerView.agreenLabel.hidden = YES;
            _headerView.agreement.hidden = YES;
            _headerView.chooseBtn.hidden = YES;
        }
        
    } failure:^(NSDictionary *error) {
        
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [YFUserStatusAndTypeTool fetchUserStatus:^(NSInteger status, NSInteger userType) {
        _userState = status;
        _user_type = userType;
    }];
    
    // 启动懒加载
    self.tableView.tableHeaderView = self.headerView;
    
}

#pragma mark -- 图片上传方法
- (void)imageUploadActionWithType:(NSString *)typeStr{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cameraAction];
    [alert addAction:photoAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    self.idcardType = typeStr;
}
#pragma mark -- 选择图片来源
- (void)gotoIamgePickerWith:(NSUInteger)sourceType{
    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [MBPHUD showAlertWithTitle:@"您没有对相机授权!"];
            return;
        }
        
    }else{
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [MBPHUD showAlertWithTitle:@"您没有对相册授权!"];
            return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:^{
    }];
}
#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    [self dismissViewControllerAnimated:YES completion:^{
        //    NSData *data = UIImageJPEGRepresentation(image, 1);
        if ([self.idcardType isEqualToString:@"one"]) {
            //正面
            [self.headerView.iDCardFront setImage:image forState:UIControlStateNormal];
            self.frontImg = image;
            
        }else if ([self.idcardType isEqualToString:@"two"]){
            //反面
            [self.headerView.iDCardBack setImage:image forState:UIControlStateNormal];
            self.backImg = image;
            
        }else if ([self.idcardType isEqualToString:@"1"]) {
            // 门店照片1
            [self.headerView.shopImgView1 setImage:image forState:UIControlStateNormal];
            self.shopImg1 = image;
            
        }else if ([self.idcardType isEqualToString:@"2"]) {
            // 门店照片2
            [self.headerView.shopImgView2 setImage:image forState:UIControlStateNormal];
            self.shopImg2 = image;
            
        }else if ([self.idcardType isEqualToString:@"3"]) {
            // 门店照片3
            [self.headerView.shopImgView3 setImage:image forState:UIControlStateNormal];
            self.shopImg3 = image;
        }else if ([self.idcardType isEqualToString:@"yyImage"]) {
            // 门店照片3
            [self.headerView.cardImgView setImage:image forState:UIControlStateNormal];
            self.yyImg = image;
        }
    }];
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        //        CGFloat top = IS_iOS11Later ? Height_NavBar : 0;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = YFColor_Background;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (YFKaiDianHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[[NSBundle mainBundle]loadNibNamed:@"YFKaiDianHeaderView" owner:self options:nil]firstObject];
        _headerView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 1500);
        WEAKSELF;
        _headerView.headerBtnClickBlock = ^(NSInteger tag) {
            NSLog(@"传递过来的tag = %ld", (long)tag);
            switch (tag) {
                case YFKaiDianHeaderViewTag_Category:
                    //商家分类
                    [weakSelf categorySelectAction];
                    break;
                case YFKaiDianHeaderViewTag_Location:
                    // 定位
                    [weakSelf location];
                    break;
                case YFKaiDianHeaderViewTag_Address:
                    [weakSelf citySelectAction];
                    break;
                case YFKaiDianHeaderViewTag_IDCardFront:
                    //身份证正面
                    [weakSelf imageUploadActionWithType:@"one"];
                    break;
                case YFKaiDianHeaderViewTag_IDCardBack:
                    //身份证正面
                    [weakSelf imageUploadActionWithType:@"two"];
                    break;
                case YFKaiDianHeaderViewTag_ShopImgView1://门店照片1
                    [weakSelf imageUploadActionWithType:@"1"];
                    break;
                case YFKaiDianHeaderViewTag_ShopImgView2://门店照片2
                    [weakSelf imageUploadActionWithType:@"2"];
                    break;
                case YFKaiDianHeaderViewTag_ShopImgView3://门店照片3
                    [weakSelf imageUploadActionWithType:@"3"];
                    break;
                case YFKaiDianHeaderViewTag_BusinessCard://营业执照
                    [weakSelf imageUploadActionWithType:@"yyImage"];
                    break;
                case YFKaiDianAgreenViewTag_BtnAgreen://同意条款
                    weakSelf.headerView.agreeBtn.selected  = !weakSelf.headerView.agreeBtn.selected;
               
                    break;
                case YFKaiDianHeaderViewTag_Agreement:
                    
                    [weakSelf gotoAgreementAction];
                    break;
                case YFKaiDianHeaderViewTag_NextStep:
                  
                    [weakSelf uploadKaiDianApplyAction];
                    break;
                default:
                    break;
            }
            
        };
    }
    return _headerView;
}
- (void)gotoAgreementAction{
    
    YFWebViewController *web = [[YFWebViewController alloc]init];
    web.urlString = @"www.quanjieshop.com/indexphp/mobile/Article/detail/article_id/28?app=1";
    KPushViewControllerAction(web);
    
}

- (void)location {
    // 百度地图
    WEAKSELF
    YFMapLocationVC *vc = [[YFMapLocationVC alloc] init];
    vc.locationAddressBlock = ^(QJLocationInfoModel *model) {
        
        self.headerView.coordinate.text = @"店铺已定位成功";
        self.headerView.coordinate.textColor = YFColor_Text;
        
        // 本来就是百度地图, 无需再转换了
        weakSelf.kdModel.longitude = [NSString stringWithFormat:@"%.6f",model.coordinate.longitude];
        weakSelf.kdModel.latitude = [NSString stringWithFormat:@"%.6f",model.coordinate.latitude];
   
    };
    
    KPushViewControllerAction(vc);
}



#pragma mark - 拼接店铺地址
- (void)fetchShopImg {
    // 拼接门店照片
    self.kdModel.image_url = @"";
    if (self.kdModel.image1_url.length) {
        self.kdModel.image_url = self.kdModel.image1_url;
        
    }else if (self.kdModel.image2_url.length) {
        
        if (self.kdModel.image_url.length) {  // , 拼接
            self.kdModel.image_url = [self.kdModel.image_url stringByAppendingString:[NSString stringWithFormat:@",%@", self.kdModel.image2_url]];
        }else {
            self.kdModel.image_url = self.kdModel.image2_url;
        }
        
    }else if (self.kdModel.image3_url.length) {
        
        if (self.kdModel.image_url.length) {  // , 拼接
            self.kdModel.image_url = [self.kdModel.image_url stringByAppendingString:[NSString stringWithFormat:@",%@", self.kdModel.image3_url]];
        }else {
            self.kdModel.image_url = self.kdModel.image3_url;
        }
    }
    NSLog(@"拼接后的 image_url = %@", self.kdModel.image_url);
}

#pragma mark -- 所在地区方法
- (void)citySelectAction{
    
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, UISCREEN_HEIGHT-250, UISCREEN_WIDTH, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        
        
        NSString *provine = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        NSString *city = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        NSString *district = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
        self.kdModel.provice = provine;
        self.kdModel.city = city;
        self.kdModel.region = district;
        NSString *str =  [NSString stringWithFormat:@"%@-%@-%@",provine,city,district];
        [self.headerView.addressBtn setTitle:str forState:UIControlStateNormal];
        
    };
    
}

#pragma mark -- 商家分类方法
- (void)categorySelectAction{
    WEAKSELF
    [[SOPopViewTool sharedInstance] popView:self.categoryView animated:YES];
    self.categoryView.categoryIDSelectBlock = ^(CategoryLeftModel *model) {
        
        [[SOPopViewTool sharedInstance]closeAnimated:YES];
        weakSelf.headerView.category.text = model.name;
        NSLog(@"model.id = %@", model.udid);
        weakSelf.headerView.categoryID = [NSString stringWithoutNilFormart:model.udid];
        weakSelf.headerView.category.textColor = YFColor_Text;
    };
    self.categoryView.cancelBtnClickBlock = ^{
        [[SOPopViewTool sharedInstance]closeAnimated:YES];
    };
}

#pragma mark -- 照片上传参数
- (NSMutableDictionary *)uploadDict {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"shop" forKey:@"savepath"];
    [dict setObject:@"banner" forKey:@"pictitle"];
    [dict setObject:@"image" forKey:@"dir"];
    return dict;
}
#pragma mark -- 正面上传
- (void)uploadImageOneAction{
    
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.iDCardFront.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"正面 = response = %@", response);
        if (response) {
            self.kdModel.idCardUrl = response[@"url"];
            [self uploadImageTwoAction];
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"身份证正面照提交失败,请重新提交!"];
    }];
}
#pragma mark -- 反面上传
- (void)uploadImageTwoAction{
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.iDCardBack.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"反面 = response = %@", response);
        if (response) {
            self.kdModel.idCardUrl2 = response[@"url"];
            
            
            if (self.shopImg1) {
                [self uploadImage1Action];
                
            }else if (self.shopImg2) {
                [self uploadImage2Action];
                
            }else if (self.shopImg3) {
                [self uploadImage3Action];
            }
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"身份证反面照提交失败,请重新提交!"];
    }];
}

#pragma mark -- 店铺照片1
- (void)uploadImage1Action{
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.shopImgView1.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"店铺照片1 = response = %@", response);
        if (response) {
            self.kdModel.image1_url = response[@"url"];
            
            if (self.shopImg2) {
                [self uploadImage2Action];
                
            }else if (self.shopImg3) {
                [self uploadImage3Action];
            }else {
                
                [self uploadyyImaAction];
            }
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"店铺照片1提交失败,请重新提交!"];
    }];
}

#pragma mark -- 店铺照片2
- (void)uploadImage2Action{
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.shopImgView2.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"店铺照片2 = response = %@", response);
        if (response) {
            self.kdModel.image2_url = response[@"url"];
            
            if (self.shopImg3) {
                [self uploadImage3Action];
            }else {
                [self uploadyyImaAction];
                // 跳转页面
                //                [self fetchShopImg];
                //                QJKDVCThree *VC = [[QJKDVCThree alloc]init];
                //                VC.kdModel = self.kdModel;
                //                [self.navigationController pushViewController:VC animated:YES];
            }
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"店铺照片2提交失败,请重新提交!"];
    }];
}

#pragma mark -- 店铺照片3
- (void)uploadImage3Action{
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.shopImgView3.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"店铺照片3 = response = %@", response);
        if (response) {
            self.kdModel.image3_url = response[@"url"];
            
            if (self.yyImg) {
                [self uploadyyImaAction];
            }
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"店铺照片3提交失败,请重新提交!"];
    }];
}
#pragma mark -- 营业执照
- (void)uploadyyImaAction{
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.shopImgView3.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"店铺照片3 = response = %@", response);
        if (response) {
            self.kdModel.companyLicenseUrl = response[@"url"];
            [self uploadKaiDianSubmint];
        }
        
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"店铺照片3提交失败,请重新提交!"];
    }];
}
- (void)uploadKaiDianSubmint{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue:self.kdModel.companyName forKey:@"companyName"];//门店名称
    [dict setValue:self.kdModel.categoryNearId forKey:@"categoryNearId"];//门店分类
    [dict setValue:self.kdModel.tel forKey:@"tel"];//门店电话
    [dict setValue:self.kdModel.linkName forKey:@"linkName"];//店主姓名
    
    [dict setValue:self.kdModel.provice forKey:@"provice"];
    [dict setValue:self.kdModel.city forKey:@"city"];
    [dict setValue:self.kdModel.region forKey:@"region"];
    [dict setValue:self.kdModel.detailAddress forKey:@"detailAddress"];
    [dict setValue:self.kdModel.longitude forKey:@"longitude"];
    [dict setValue:self.kdModel.latitude forKey:@"latitude"];
    [dict setValue:self.kdModel.legalPerson forKey:@"legalPerson"];//法人姓名
    
    [dict setValue:self.kdModel.companyLicenseCode forKey:@"companyLicenseCode"];
    [dict setValue:self.kdModel.idCardUrl forKey:@"idCardUrl"];
    [dict setValue:self.kdModel.idCardUrl2 forKey:@"idCardUrl2"];
    [dict setValue:self.kdModel.image1_url forKey:@"image_url"];
    [dict setValue:self.kdModel.image2_url forKey:@"image2_url"];
    [dict setValue:self.kdModel.image3_url forKey:@"image3_url"];
    [dict setValue:self.kdModel.companyLicenseUrl forKey:@"companyLicenseUrl"];
    
    [dict setObject:self.kdModel.legalPerson forKey:@"legalPerson"];
    [dict setObject:self.kdModel.legalPerson forKey:@"userName"];
    [dict setObject:self.kdModel.legalMobel forKey:@"mobile"];
    [dict setObject:self.kdModel.cardNum  forKey:@"shopName"];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setObject:self.kdModel.legalPerson forKey:@"user_name"];
    [params setObject:self.kdModel.legalMobel forKey:@"mobile"];
    [params setObject:self.kdModel.cardNum  forKey:@"card_num"];
    
    [WWNetRequest fetchOrderBindcardWithParameters:params success:^(id response) {
        
    } failure:^(NSDictionary *error) {
        
    }];
    
    [WWNetRequest fetchOpenShopWithParameters:dict success:^(id response) {
        
        [MBPHUD showAlertWithTitle:@"您的申请已提交成功,请等待系统审核!"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSDictionary *error) {
        NSLog(@"开店 error = %@", error);
    }];
}
#pragma mark -- 提交开店申请
- (void)uploadKaiDianApplyAction{
    
   
    //数据处理
    self.kdModel.companyName = self.headerView.companyName.text;  // 门店名称
    //    self.kdModel.shopName = self.headerView.companyName.text;  // 先跟companyName一样
    
    self.kdModel.categoryNearId = self.headerView.categoryID;//门店分类
    self.kdModel.tel = self.headerView.phone.text;//门店电话
    self.kdModel.linkName = self.headerView.linkName.text;//店主姓名
    self.kdModel.detailAddress = self.headerView.addressDetail.text;//详细地址
    
    self.kdModel.legalPerson = self.headerView.legalEntityName.text;//法人姓名
    self.kdModel.legalMobel =  self.headerView.legalEntityPhone.text;// 法人电话
    self.kdModel.cardNum = self.headerView.accountNumber.text; // 结算账号
    self.kdModel.companyLicenseCode = self.headerView.cardNum.text;//执照编号
    
    if (self.type == YFKaiDianVCType_Edit) {
        
        [self uploadEditAction];
        
        return;
        
    }
    if (!_headerView.agreeBtn.selected) {
        
        [MBPHUD showAlertWithTitle:@"请勾选泉界商城入驻协议"];
        
        return;
    }
    if (self.kdModel.latitude == 0 || self.kdModel.longitude == 0) { // 位置判断
        [MBPHUD showAlertWithTitle:@"请标记店铺坐标"];
        
    }else if (!self.frontImg || !self.backImg) { // 身份证
        [MBPHUD showAlertWithTitle:@"请上传身份证照片"];
        
    }else if (!(self.shopImg1 || self.shopImg2 || self.shopImg3)) { // 店铺照片
        [MBPHUD showAlertWithTitle:@"请上传门店照片, 至少一张"];
        
    }else if (self.kdModel.provice.length == 0 ||
              self.kdModel.city.length == 0 ||
              self.kdModel.region.length == 0) { // 所在地区, 判断一个就够了
        [MBPHUD showAlertWithTitle:@"请选择店铺所在地区"];
        
    }else if (self.kdModel.companyName.length == 0 ||
              self.kdModel.categoryNearId.length == 0 ||
              self.kdModel.tel.length == 0 ||
              self.kdModel.linkName.length == 0 ||
              self.kdModel.detailAddress.length == 0 ||
              self.kdModel.legalPerson.length == 0 ||self.kdModel.companyLicenseCode.length == 0) {
        [MBPHUD showAlertWithTitle:@"请检查所需信息是否完整!"];
    }else {
        
        
        [self uploadImageOneAction];
        
    }
    
}
- (void)uploadEditAction{
    
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:[self uploadDict] withImageFile:self.headerView.shopImgView1.imageView.image hiddenHUD:YES HUDTitle:@"正在提交信息..." success:^(NSURLSessionDataTask *operetion, id response) {
        NSLog(@"店铺照片1 = response = %@", response);
        if (response) {
            self.kdModel.image1_url = response[@"url"];
            [self uploadInfomation];
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        [MBPHUD showAlertWithTitle:@"店铺照片1提交失败,请重新提交!"];
    }];
    
    
}
- (void)uploadInfomation{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.kdModel.tel forKey:@"tel"];
    [dict setObject:self.kdModel.image1_url forKey:@"image_url"];
    [dict setObject:self.kdModel.detailAddress forKey:@"detail_address"];
    if ([self.kdModel.longitude isEqualToString:@""]||self.kdModel.longitude== nil) {
         [dict setObject:self.ckdpModel.longitude forKey:@"longitude"];
    }else{
         [dict setObject:self.kdModel.longitude forKey:@"longitude"];
    }
    if ([self.kdModel.latitude isEqualToString:@""]||self.kdModel.latitude == nil) {
        
        [dict setObject:self.ckdpModel.latitude forKey:@"latitude"];
    }else{
         [dict setObject:self.kdModel.latitude forKey:@"latitude"];
    }
   
    
    [WWNetRequest fetchEditShopWithParameters:nil success:^(id response) {
        
        if (response) {
            
            [MBPHUD showAlertWithTitle:@"提交成功"];
            
        }
        
    } failure:^(NSDictionary *error) {
        
       
        
    }];
}
#pragma mark - 懒加载
- (QJKDCategorySelectView *)categoryView{
    if (!_categoryView) {
        _categoryView = [[[NSBundle mainBundle]loadNibNamed:@"QJKDCategorySelectView" owner:self options:nil] firstObject];
        _categoryView.frame = CGRectMake(0, 0, UISCREEN_WIDTH - 40, 300);
        [_categoryView setupSelectUIAction];
    }
    return _categoryView;
}
//- (void)test{
//
//    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(39.90868, 116.3956);//原始坐标
//    //转换 google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标至百度坐标
//
//    NSDictionary* testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_COMMON);
//    //转换GPS坐标至百度坐标(加密后的坐标)
//    testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_GPS);
//    NSLog(@"x=%@,y=%@",[testdic objectForKey:@"x"],[testdic objectForKey:@"y"]);
//    //解密加密后的坐标字典
//    CLLocationCoordinate2D baiduCoor = BMKCoorDictionaryDecode(testdic);
//
//}
@end

